<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="例如：测试"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input class="w500" v-model="form.description" placeholder="例如：ceshi"></el-input>
      </el-form-item>
      <el-form-item label="标识" prop="action">
        <el-input class="w500" v-model="form.action" ></el-input>
      </el-form-item>
      <el-form-item label="图标">
        <el-input class="w500" v-model="form.profile_pic" ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="order">
        <el-input class="w100" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item label="是否公开">
        <el-radio-group v-model="form.is_public">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data: function () {
      return {
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ],
          action: [
            {required: true, message: '请输入标识', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      ...mapState({
        form: state => state.favorite.classInfo
      })
    },
    async created () {
      await this.classInfo()
      this.form.is_public = String(this.form.is_public)
    },
    methods: {
      // 获取分类详情
      async classInfo () {
        let payload = {
          id: this.$route.params.id
        }
        await this.$store.dispatch('favorite/classInfo', payload)
      },
      // 编辑分类
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('favorite/classEdit', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/favorite/tree')
            })
          }
        })
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
